package com.example.servlet.user;

import com.example.entity.User;
import com.example.service.ManagerService;
import com.example.service.UserService;
import com.example.service.impl.ManagerServiceImpl;
import com.example.service.impl.UserServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;

@WebServlet("/user_ticket")
public class uTicketServlet extends HttpServlet {
    private UserService service;
    private SqlSession sqlSession;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new UserServiceImpl(sqlSession);
    }

    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try (SqlSession newSession = MybatisUtil.getSession()) {
            UserService freshService = new UserServiceImpl(newSession);
            User user = (User) req.getSession().getAttribute("user");
            int u_id = user.getU_id();
            int p_id = freshService.getPidByUid(u_id);
            Context context = new Context();
            if (req.getSession().getAttribute("not_sufficient_funds") != null) {
                context.setVariable("not_sufficient_funds", true);
                req.getSession().removeAttribute("not_sufficient_funds");
            }
            if (req.getSession().getAttribute("pay_success") != null) {
                context.setVariable("pay_success", true);
                req.getSession().removeAttribute("pay_success");
            }
            if (req.getSession().getAttribute("pay_unsuccessful") != null) {
                context.setVariable("pay_unsuccessful", true);
                req.getSession().removeAttribute("pay_unsuccessful");
            }
            if (req.getSession().getAttribute("ticket_operation_failure") != null) {
                context.setVariable("ticket_operation_failure", true);
                req.getSession().removeAttribute("ticket_operation_failure");
            }
            context.setVariable("user", user);
            context.setVariable("user_grade", user.getGrade());
            context.setVariable("tickets", freshService.getTicketDetailsListByPassengerId(p_id));
            context.setVariable("allTicketCounts", freshService.getTicketNumByPassengerId(p_id));
            context.setVariable("upcomingTicketCounts", freshService.getUpcomingTicketNumByPassengerId(p_id));
            context.setVariable("completedTicketCounts", freshService.getCompletedTicketNumByPassengerId(p_id));
            context.setVariable("cancelledTicketCounts", freshService.getCancelledTicketNumByPassengerId(p_id));
            context.setVariable("plannedTicketCounts", freshService.getPlannedTicketNumByPassengerId(p_id));
            context.setVariable("unpaidTicketCounts", freshService.getUnpaidTicketNumByPassengerId(p_id));
            context.setVariable("refundTicketCounts", freshService.getRefundTicketNumByPassengerId(p_id));
            context.setVariable("delayTicketCounts", freshService.getDelayTicketNumByPassengerId(p_id));
            // 图片是否存在判断
            String imagePath = getServletContext().getRealPath("/static/images/" + user.getU_id() + ".jpg");
            File imageFile = new File(imagePath);
            boolean imageExists = imageFile.exists();
            context.setVariable("imageExists", imageExists);

            ThymeleafUtil.process("user_ticket.html", context, resp.getWriter());
        }
    }
}
